package com.fqcar.fqdf.ReportingProblems;

import android.app.Activity;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.fqcar.fqdf.R;
import com.fqcar.fqdf.entity.CarPhotoBean;
import com.fqdf.framework.android.adapter.BaseRecycleAdapter;
import com.fqdf.framework.android.view.listener.RecycleItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * 上报问题-上传图片的adapter
 */
public class ReportingProblemsImageAdapter extends BaseRecycleAdapter<CarPhotoBean, ReportingProblemsImageAdapter.UploadImgHolder> {

    private int maxCount;

    private TextView uploadImgTxt;

    public ReportingProblemsImageAdapter(Activity mContext, int maxCount, TextView uploadImgTxt) {
        super(mContext);
        this.uploadImgTxt = uploadImgTxt;
        this.maxCount = maxCount;
        List<CarPhotoBean> list = new ArrayList<>();
        list.add(obtainNewItem());
        setList(list);

        updateUploadImgText();
    }


    @Override
    public ReportingProblemsImageAdapter.UploadImgHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ReportingProblemsImageAdapter.UploadImgHolder(getLayoutInflater().inflate(R.layout.activity_report_problems_upload_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ReportingProblemsImageAdapter.UploadImgHolder holder, final int position) {

        final CarPhotoBean item = getItem(position);

        if (item.isCameraPhoto()) {
            Glide.with(getContext()).load(item.getUrl()).fitCenter().into(holder.uploadImageCamera);
            holder.uploadImageDel.setVisibility(View.VISIBLE);
        } else {
            holder.uploadImageCamera.setImageResource(R.drawable.icon_upload_img_bg);
            holder.uploadImageDel.setVisibility(View.GONE);
        }

        holder.uploadImageDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int cameraCount = getCameraCount();
                removeItem(position);
                if (cameraCount == maxCount) {
                    addItem(obtainNewItem());
                }
                updateUploadImgText();
                notifyDataSetChanged();
            }
        });

        holder.uploadImageCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!item.isCameraPhoto() && getContext() instanceof RecycleItemClickListener) {
                    RecycleItemClickListener itemClickListener = (RecycleItemClickListener) getContext();
                    itemClickListener.onItemClick(v, position);
                }
            }
        });

    }

    /**
     * 获取当前拍照的照片数量
     *
     * @return 照片数量
     */
    private int getCameraCount() {
        return getCameraList().size();
    }

    /**
     * 获取当前拍照的图片
     *
     * @return
     */
    public List<CarPhotoBean> getCameraList() {
        List<CarPhotoBean> list = new ArrayList<>();
        for (CarPhotoBean item : getOriginList()) {
            if (item.isCameraPhoto()) {
                list.add(item);
            }
        }
        return list;
    }

    /**
     * 更新item对应的图片,并增加一个新的item到最后
     *
     * @param position item的位置
     * @param url      图片的url
     */
    public void updateItem(int position, String url) {

        CarPhotoBean item = getItem(position);
        item.setUrl(url);
//        item.setPath(path);
        item.setCameraPhoto(true);

        if (position < maxCount - 1) {
            addItem(obtainNewItem());
        }

        updateUploadImgText();

    }

    private CarPhotoBean obtainNewItem() {
        CarPhotoBean item = new CarPhotoBean();
        item.setCameraPhoto(false);
        return item;
    }

    /**
     * 更新上传图片内容的text
     */
    private void updateUploadImgText() {
        if (uploadImgTxt != null) {
            uploadImgTxt.setText(getContext().getString(R.string.upload_pic_txt, getCameraCount() + "/" + maxCount));
        }
    }


    public static class UploadImgHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.upload_image_camera)
        AppCompatImageView uploadImageCamera;
        @BindView(R.id.upload_image_del)
        AppCompatImageView uploadImageDel;

        UploadImgHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
